package com.youmu.maven.emall.Util;

import java.lang.reflect.Field;

public class ReflectUtils {

	public static Object get(Object target, String propName)
			throws NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException {
		Field f = null;
		Class clazz = target.getClass();
		while (clazz != Object.class) {
			try {
				f = clazz.getDeclaredField(propName);
				f.setAccessible(true);
				return f.get(target);
			} catch (NoSuchFieldException e) {
				clazz = clazz.getSuperclass();
			}
		}
		throw new NoSuchFieldException(propName);
	}

	public static void set(Object target, String propName, Object value)
			throws NoSuchFieldException, SecurityException,
			IllegalArgumentException, IllegalAccessException {
		Field f = null;
		Class clazz = target.getClass();
		while (clazz != Object.class) {
			try {
				f = clazz.getDeclaredField(propName);
				f.setAccessible(true);
				f.set(target, value);
				return;
			} catch (NoSuchFieldException e) {
				clazz = clazz.getSuperclass();
			}
		}
		throw new NoSuchFieldException(propName);
	}

}
